package com.dream.service.user.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by kangd on 2016/4/6.
 */
public class ConfigUtil {

    private static final Log logger = LogFactory.getLog(ConfigUtil.class);

    static Properties properties;

    static {
        InputStream inputStream = null;
        try {
            inputStream  = ConfigUtil.class.getClassLoader().getResourceAsStream("system.properties");
            properties = new Properties();
            properties.load(inputStream);
        } catch (Exception e) {
            logger.error("properties is not found", e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                logger.error("load properties failed", e);
            }
        }
    }
    public static String getPro(String key){
        return getPro(key, "");
    }
    public static String getPro(String key,String defaultvalue){
        String value= properties.getProperty(key);
        if(value==null || value.trim().equals("")){
            value=defaultvalue;
        }
        return value;
    }
    public static int getInt(String key,int defaultvalue){
        String value= properties.getProperty(key);
        boolean useDefault=false;
        if(value==null || value.trim().equals("")){
            useDefault=true;
        }
        return useDefault?defaultvalue:Integer.parseInt(value);
    }
    public static int getInt(String key){
        String value= properties.getProperty(key);
        return Integer.parseInt(value);
    }

    public static boolean getBoolean(String key){
        String value= properties.getProperty(key);
        return "true".equals(value);
    }
    public  static void main(String[] args){
        System.out.println(getPro("default_icon"));
    }
}
